package com.joysuch.wwyt.mh.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.safetyworkcert.BpWorkRiskDto;
import com.joysuch.wwyt.bp.entity.AlarmMessage;
import com.joysuch.wwyt.bp.entity.BpDeviceFacilityCollection;
import com.joysuch.wwyt.bp.entity.BpDeviceFacilityMonitoringPoint;
import com.joysuch.wwyt.bp.entity.InspectExceptionAlarmMesage;
import com.joysuch.wwyt.bp.entity.vo.AlarmMessageVO;
import com.joysuch.wwyt.bp.enums.ApiAlarmTypeEnum;
import com.joysuch.wwyt.bp.mapper.AlarmMessageMapper;
import com.joysuch.wwyt.bp.mapper.InspectExceptionAlarmMesageMapper;
import com.joysuch.wwyt.bp.repository.BpDeviceFacilityCollectionDao;
import com.joysuch.wwyt.bp.repository.BpDeviceFacilityMonitoringPointDao;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.common.util.DateTimeUtil;
import com.joysuch.wwyt.core.common.util.JsonUtil;
import com.joysuch.wwyt.core.common.util.MapUtils;
import com.joysuch.wwyt.core.enums.RiskLevels;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.bean.*;
import com.joysuch.wwyt.mh.entity.MhAlarmMessage;
import com.joysuch.wwyt.mh.entity.MhMajorHazard;
import com.joysuch.wwyt.mh.entity.MhMonitoringPoint;
import com.joysuch.wwyt.mh.entity.MhMonitoringType;
import com.joysuch.wwyt.mh.repository.MhAlarmMessageDao;
import com.joysuch.wwyt.mh.repository.MhMajorHazardDao;
import com.joysuch.wwyt.mh.repository.MhMonitoringPointDao;
import com.joysuch.wwyt.mh.service.CustomizeMonitorPointService;
import com.joysuch.wwyt.mh.service.MonitorPointDataWebSocketService;
import com.joysuch.wwyt.mh.websocket.*;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskEvaluationAlarmMesage;
import com.joysuch.wwyt.risk.entity.RiskEvaluationControl;
import com.joysuch.wwyt.risk.entity.RiskPoint;
import com.joysuch.wwyt.risk.mapper.RiskPointMapper;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.joysuch.wwyt.risk.repository.RiskEvaluationAlarmMesageDao;
import com.joysuch.wwyt.risk.repository.RiskEvaluationControlDao;
import com.joysuch.wwyt.risk.repository.RiskPointDao;
import com.joysuch.wwyt.risk.service.RiskPointService;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.Tuple;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MonitorPointDataWebSocketServiceImpl implements MonitorPointDataWebSocketService {


    @Autowired
    private MhAlarmMessageDao messageDao;

    @Autowired
    private MhMonitoringPointDao monitoringPointDao;

    @Autowired
    private BpDeviceFacilityMonitoringPointDao bpDeviceFacilityMonitoringPointDao;

    @Autowired
    private RiskEvaluationAlarmMesageDao riskEvaluationAlarmMesageDao;

    @Autowired
    private RiskDistrictDao riskDistrictDao;

    @Autowired
    private BpDeviceFacilityCollectionDao bpDeviceFacilityCollectionDao;

    @Autowired
    private MhMajorHazardDao mhMajorHazardDao;

    @Autowired
    private CustomizeMonitorPointService customizeMonitorPointService;

    @Autowired
    private InspectExceptionAlarmMesageMapper inspectExceptionAlarmMesageMapper;

    @Autowired
    private RiskPointMapper riskPointMapper;

    @Autowired
    private WorkFlowProcessService workFlowProcessService;

    @Autowired
    private RiskPointService riskPointService;

    @Autowired
    private AlarmMessageMapper alarmMessageMapper;

    @Autowired
    private RiskPointDao riskPointDao;

    @Autowired
    private RiskEvaluationControlDao riskEvaluationControlDao;

    @Value("${wwyt.base.serverUrl}")
    private String serverUrl;

    /**
     * @param messages 危险源报警信息
     * @param flagType ALL 所有报警  ADD 产生报警  HANDLE 处理报警
     */
    @Override
    public void getPointAlarmMessage(List<MhAlarmMessage> messages, String flagType) {
        try {
            Map<Long, List<Long>> map = new HashMap<>();
            List<Long> deviceIds = bpDeviceFacilityMonitoringPointDao.findAllDeviceId();
            for (Long deviceId : deviceIds) {
                List<Long> pointIds = bpDeviceFacilityMonitoringPointDao.getMonitoringPointIdsByDeviceId(deviceId);
                if (!CollectionUtils.isEmpty(pointIds)) {
                    map.put(deviceId, pointIds);
                }
            }
            Map<Long, MhAlarmMessage> hMap = new LinkedHashMap<>();
            List<MhAlarmMessage> alarmList = new ArrayList<>();
            if (flagType.equals("ALL")) {
                List<Long> pointIds = messageDao.findPointIdsByState(2);
                if (!CollectionUtils.isEmpty(pointIds)) {
                    hMap = getAlarmMessageByPointIds(pointIds, hMap, map);
                    if (hMap.size() > 0) {
                        alarmList = hMap.values().stream().collect(Collectors.toList());
                    }

                }
            } else if (flagType.equals("ADD")) {
                if (!CollectionUtils.isEmpty(messages)) {
                    List<Long> alarmsIds = messages.stream().map(e -> e.getId()).collect(Collectors.toList());
                    List<Long> pointIds = messageDao.getPointIdsByIds(alarmsIds);
                    hMap = getAlarmMessageByPointIds(pointIds, hMap, map);
                    if (hMap.size() > 0) {
                        alarmList = hMap.values().stream().collect(Collectors.toList());
                    }
                }
            } else if (flagType.equals("HANDLE")) {
                alarmList = messages;
            }
            if (!CollectionUtils.isEmpty(alarmList)) {
                setMonitorPointDataWebSocketAlarmData(alarmList, flagType);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


    private Map<Long, MhAlarmMessage> getAlarmMessageByPointIds(List<Long> ids, Map<Long, MhAlarmMessage> hMap, Map<Long, List<Long>> map) {
        for (Long pointId : ids) {
            Optional<MhMonitoringPoint> op = monitoringPointDao.findById(pointId);
            if (op.isPresent()) {
                MhMonitoringPoint mhMonitoringPoint = op.get();
                if (mhMonitoringPoint.getMhMonitoringType() != null) {
                    MhMonitoringType type = mhMonitoringPoint.getMhMonitoringType();
                    if (type.getCategoryId() != null && type.getCategoryId().intValue() == 0) {
                        //设备最近一条报警
                        BpDeviceFacilityMonitoringPoint fp = bpDeviceFacilityMonitoringPointDao.findFirstByMonitoringPointId(mhMonitoringPoint.getId());
                        if (fp != null && fp.getDeviceId() != null) {
                            List<Long> pointIds = map.get(fp.getDeviceId());
                            if (!CollectionUtils.isEmpty(pointIds)) {
                                MhAlarmMessage mess = messageDao.getLastAlarmMessageByPointIds(pointIds);
                                if (mess != null) {
                                    hMap.put(fp.getDeviceId(), mess);
                                }
                            }
                        }
                    } else if (type.getCategoryId() != null && type.getCategoryId().intValue() == 1) {
                        //监测点最近一条报警
                        MhAlarmMessage mess = messageDao.getLastAlarmMessageByPointIds(Lists.newArrayList(mhMonitoringPoint.getId()));
                        if (mess != null) {
                            hMap.put(pointId, mess);
                        }
                    }
                }
            }
        }
        return hMap;
    }


    /**
     * 大屏推送危险源报警信息
     */
    @Override
    public void setMonitorPointDataWebSocketAlarmData(List<MhAlarmMessage> alarmList, String flagType) {
        try {
            List<MonitorWebSocketPointAlarmData> datas = new ArrayList<>();
            //获取报警消息
            if (!CollectionUtils.isEmpty(alarmList)) {
                for (MhAlarmMessage message : alarmList) {
                    if (message == null) {
                        continue;
                    }
                    MonitorWebSocketPointAlarmData data = new MonitorWebSocketPointAlarmData();
                    data.setTenentId(message.getTenentId());
                    data.setOrgCode(message.getOrgCode());
                    data.setAlarmMessageId(message.getId());
                    data.setFlagType(flagType);
                    data.setAlarmMessageTime(DateTimeUtil.localDateTime2Date(message.getAlarmTime()));
                    //报警的监测点信息 message.getMhMonitoringPoint()会有懒加载问题
                    Long pointId = messageDao.getPointId(message.getId());
                    if (pointId != null) {
                        Optional<MhMonitoringPoint> omp = monitoringPointDao.findById(pointId);
                        if (omp.isPresent()) {
                            MhMonitoringPoint mhMonitoringPoint = omp.get();
                            data = covertPointAlarmData(mhMonitoringPoint, data, message);
                        }
                    }
                    datas.add(data);
                }
            }
            MonitorPointDataWebSocket.sendInfo(datas, WebsocketDataType.POINT_ALARM.getIndex());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public MonitorWebSocketPointAlarmData covertPointAlarmData(MhMonitoringPoint mhMonitoringPoint, MonitorWebSocketPointAlarmData data, MhAlarmMessage message) {
        data.setClassify("POINT");
        data.setPointId(mhMonitoringPoint.getId());
        data.setAlarmMessage(mhMonitoringPoint.getName() + message.getDetail());
        data.setLocation(mhMonitoringPoint.getLocation());
        data.setLocation(mhMonitoringPoint.getLocation());
        data.setLocationType(0);
        MhMonitoringType mhMonitoringType = mhMonitoringPoint.getMhMonitoringType();
        if (mhMonitoringType != null) {
            data.setPointTypeName(mhMonitoringType.getName());
            data.setPointTypeCode(mhMonitoringType.getCode());
        }
        //设备id
        BpDeviceFacilityMonitoringPoint mp = bpDeviceFacilityMonitoringPointDao.findFirstByMonitoringPointId(mhMonitoringPoint.getId());
        if (mp != null) {
            data.setDeviceId(mp.getDeviceId());
            if (mp.getDeviceId() != null) {
                Optional<BpDeviceFacilityCollection> obc = bpDeviceFacilityCollectionDao.findById(mp.getDeviceId());
                if (obc.isPresent()) {
                    BpDeviceFacilityCollection collection = obc.get();
                    if (collection.getDeviceTypeName().equals("储罐区")) {
                        data.setLocationType(1);
                    } else {
                        data.setLocationType(0);
                    }
                    data.setClassify("FACILITY");
                    data.setLocation(collection.getCoordinates());
                    data.setDeviceTypeName(collection.getDeviceTypeName());
                    data.setMhHazardId(collection.getMajorHazardId());
                    if (collection.getMajorHazardId() != null) {
                        Optional<MhMajorHazard> omh = mhMajorHazardDao.findByIdAndEfficient(collection.getMajorHazardId());
                        if (omh.isPresent()) {
                            data.setClassify("HAZARD");
                            data.setLocation(omh.get().getLocation());
                            data.setLocationType(0);
                        }
                    }
                }
            }
        }

        return data;
    }

    /**
     * @param messages 区域报警信息
     * @param flagType ALL 所有报警  ADD 产生报警  HANDLE 处理报警
     */
    @Override
    public void getDistrictAlarmMessage(List<RiskEvaluationAlarmMesage> messages, String flagType) {
        try {
            List<RiskEvaluationAlarmMesage> alarmList = new ArrayList<>();
            if (flagType.equals("ALL")) {
                List<Long> districtIds = riskEvaluationAlarmMesageDao.findDistrictIdsByState(1);
                if (!CollectionUtils.isEmpty(districtIds)) {
                    alarmList = getAlarmMessageByDistrictIds(districtIds, alarmList);
                }
            } else if (flagType.equals("ADD")) {
                for (RiskEvaluationAlarmMesage message : messages) {
                    if (message.getRiskDistrictId() != null) {
                        alarmList = getAlarmMessageByDistrictIds(Lists.newArrayList(message.getRiskDistrictId()), alarmList);
                    }
                }
            } else if (flagType.equals("HANDLE")) {
                alarmList = messages;
            }
            if (!CollectionUtils.isEmpty(alarmList)) {
                setWebSocketDistrictAlarmData(alarmList, flagType);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }

    /**
     * @param messages 巡检排查报警信息
     * @param flagType ALL 所有报警  ADD 产生报警  HANDLE 处理报警
     */
    @Override
    public void getInspectAlarmMessage(List<InspectExceptionAlarmMesage> messages, String flagType, Long tenentId, String orgCode) {
        try {
            Long curTenentId = null;
            String curOrgcode = null;
            if (StringUtils.isNotBlank(orgCode) && null != tenentId) {
                curTenentId = tenentId;
                curOrgcode = orgCode;
            } else {
                curTenentId = messages.get(0).getTenentId();
                curOrgcode = messages.get(0).getOrgCode();
            }
            List<InspectExceptionAlarmMesage> inspectList = new ArrayList<>();
            switch (flagType) {
                case "ALL":
                    inspectList = inspectExceptionAlarmMesageMapper.findLastInspectAlarmMessage(curTenentId, curOrgcode);
                    break;
                case "ADD":
                    for (InspectExceptionAlarmMesage message : messages) {
                        InspectExceptionAlarmMesage alarmMessageLast = getAlarmMessageLast(curTenentId, curOrgcode, message.getRiskPointId());
                        if (BeanUtil.isNotEmpty(alarmMessageLast)) {
                            inspectList.add(alarmMessageLast);
                        }
                    }
                    break;
                case "HANDLE":
                    inspectList = messages;
                    break;
            }
            setWebSocketInspectAlarmData(inspectList, flagType, curTenentId, curOrgcode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void getAIAlarmMessage(List<AlarmMessage> messages, String flagType, Long tenentId, String orgCode) {
        try {
            Long curTenentId = null;
            String curOrgcode = null;
            if (StringUtils.isNotBlank(orgCode) && null != tenentId) {
                curTenentId = tenentId;
                curOrgcode = orgCode;
            } else {
                curTenentId = messages.get(0).getTenentId();
                curOrgcode = messages.get(0).getOrgCode();
            }
            List<AlarmMessage> alarmMessageList = new ArrayList<>();
            switch (flagType) {
                case "ALL":
                    alarmMessageList = alarmMessageMapper.getLastAIAlarmMessageByCamerName(curTenentId, curOrgcode);
                    break;
                case "ADD":
                    for (AlarmMessage message : messages) {
                        AlarmMessage aiAlarmMessageLast = getAIAlarmMessageLast(curTenentId, curOrgcode, message.getCameraName());
                        if (!BeanUtil.isEmpty(aiAlarmMessageLast)) {
                            alarmMessageList.add(aiAlarmMessageLast);
                        }
                    }
                    break;
                case "HANDLE":
                    alarmMessageList = messages;
                    break;
            }
            setWebSocketAIAlarmData(alarmMessageList, flagType, curTenentId, curOrgcode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void getDistrictList(List<RiskDistrict> districtList, String flagType, String orgCode, Long tenentId) {
        try {
            if (flagType.equals("ALL")) {
                districtList = riskDistrictDao.findAllByOrgCodeAndTenentId(orgCode.substring(0, 7), tenentId);
            }
            if (!CollectionUtils.isEmpty(districtList)) {
                RiskEvaluationControl dyCont = riskEvaluationControlDao.findByNameAndOrgCodeAndTenentId("风险四色图计算逻辑", orgCode.substring(0, 7), tenentId);
                List<RiskDistrictScreenListBean> beanList = convertRiskDistrictScreenBean(districtList, flagType, dyCont);
                MonitorPointDataWebSocket.sendInfo(beanList, WebsocketDataType.RISK_DISTRICT_LIST.getIndex());

                RiskDistrictMapBean mapBean = convertRiskDistrictMapBean(orgCode.substring(0, 7), tenentId, dyCont);
                MonitorPointDataWebSocket.sendInfo(Lists.newArrayList(mapBean), WebsocketDataType.RISK_DISTRICT_MAP.getIndex());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private RiskDistrictMapBean convertRiskDistrictMapBean(String orgCode, Long tenentId, RiskEvaluationControl dyCont) {
        RiskDistrictMapBean mapBean = new RiskDistrictMapBean();
        List<RiskDistrict> all = riskDistrictDao.findAllByOrgCodeAndTenentId(orgCode, tenentId);
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>(4);
        Map<String, Long> countMap = new HashMap<>();
        if (dyCont != null && dyCont.getSwitchState() == 0) {
            countMap = all.stream().filter(r -> r.getStableRiskLevel() != null).collect(Collectors.groupingBy(r ->
                    RiskLevels.findRiskLevelName(r.getStableRiskLevel()), Collectors.counting()));
        } else {
            countMap = all.stream().filter(r -> RiskLevels.getRealLevelIntValue(r.getRealTimeRiskLevel()) != null).collect(Collectors.groupingBy(r ->
                    RiskLevels.getRealLevelIntValueName(r.getRealTimeRiskLevel()), Collectors.counting()));
        }
        for (RiskLevels r : RiskLevels.values()) {
            Map<String, Object> m = new HashMap<>();
            m.put("name", r.getName());
            if (countMap.get(r.getName()) != null) {
                m.put("value", countMap.get(r.getName()).intValue());
            } else {
                m.put("value", 0);
            }
            list.add(m);
        }
        Map<String, List<RiskDistrict>> collect = all.stream().collect(Collectors.groupingBy(RiskDistrict::getName));
        map.put("areaDangerCount", list);
        mapBean.setMap(map);
        mapBean.setOrgCode(orgCode);
        mapBean.setTenentId(tenentId);
        return mapBean;
    }


    @Override
    public void getRiskPointCount(String orgCode, Long tenentId) {
        try {
            int totalPointCount = 0;
            //获取所有审核通过的风险点
            List<RiskPoint> points = riskPointDao.findAllBySharedAndOrgCodeAndTenentId(3, orgCode, tenentId);
            if (!CollectionUtils.isEmpty(points)) {
                totalPointCount = points.size();
            }
            RiskPointCountBean bean = new RiskPointCountBean();
            bean.setTotalCount(totalPointCount);
            bean.setOrgCode(orgCode);
            bean.setTenentId(tenentId);
            MonitorPointDataWebSocket.sendInfo(Lists.newArrayList(bean), WebsocketDataType.RISK_POINT_COUNT.getIndex());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private List<RiskDistrictScreenListBean> convertRiskDistrictScreenBean(List<RiskDistrict> districtList, String flagType, RiskEvaluationControl dyCont) {
        List<RiskDistrictScreenListBean> beanList = new ArrayList<>();
        for (RiskDistrict district : districtList) {
            if (!flagType.equals("HANDLE")&&(district.getDeleteFlag().equals("1") || (district.getState() != null && district.getState().intValue() == 44))) {
                continue;
            }
            RiskDistrictScreenListBean bean = new RiskDistrictScreenListBean();
            bean.setId(district.getId());
            bean.setName(district.getName());
            bean.setOrgCode(district.getOrgCode());
            bean.setTenentId(district.getTenentId());
            bean.setLocation(district.getCoordinates());
            bean.setRiskFlag(dyCont.getSwitchState());
            //默认取实时等级,动态等级开关打开 则取动态等级
            Integer level = district.getStableRiskLevel();
            bean.setFlagType(flagType);
            int risingMark = 0;
            if (dyCont.getSwitchState() == 1) {
                if (district.getRealTimeRiskLevel() != null) {
                    level = RiskLevels.getRealLevelIntValue(district.getRealTimeRiskLevel());
                } else {
                    level = null;
                }
                if (level != null && district.getRiskLevel() != null) {
                    if (level < district.getRiskLevel()) {
                        risingMark = 1;
                    }
                }
            }
            bean.setRisingMark(risingMark);
            bean.setLevel(level);
            bean.setLevelName(RiskLevels.findRiskLevelName(level));
            if (!flagType.equals("HANDLE")) {
                //查询风险区域报警未处理数量 0正常 1异常
                bean.setStatus(riskEvaluationAlarmMesageDao.countDistrictNumber(district.getId(), 0) > 0 ? 1 : 0);
                bean.setDangerCount(riskPointDao.pointCountInRiskDistrict(district.getId()).intValue());
            }
            beanList.add(bean);
        }
        return beanList;
    }

    @Override
    public void sendCustomizeMonitorPoint() {
        customizeMonitorPointService.pushMonitorPoint();
    }

    @Override
    public void getWorkflowView(String orgCode, Long tenentId, String bizName) {
        try {
            Object data = workFlowProcessService.workFlowScreenIconByCompany(orgCode, tenentId, bizName).getData();
            MonitorWebsocketDto websocketDto = null;
            if (StringUtils.isBlank(bizName)) {
                websocketDto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_WORKFLOW_VIEW_ALL.getBusEnType(), data);
            } else {
                websocketDto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_WORKFLOW_VIEW_UPDATE.getBusEnType(), data);
            }
            MonitorPointDataWebSocket.sendData(tenentId + "_" + orgCode + "_" + WebsocketSubType.WORKFLOW_VIEW.getSubType(), JSON.toJSONString(websocketDto));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    //根据区域id 获取区域的最新报警
    private List<RiskEvaluationAlarmMesage> getAlarmMessageByDistrictIds(List<Long> districtIds, List<RiskEvaluationAlarmMesage> alarmList) {
        for (Long districtId : districtIds) {
            RiskEvaluationAlarmMesage message = riskEvaluationAlarmMesageDao.getLastAlarmMeaasgeByDistrictId(districtId);
            if (message != null) {
                alarmList.add(message);
            }
        }
        return alarmList;
    }

    //获取巡检风险点最新的报警
    private InspectExceptionAlarmMesage getAlarmMessageLast(Long tenentId, String orgCode, Long pointId) {
        return inspectExceptionAlarmMesageMapper.getLastAlarmMeaasgeLast(tenentId, orgCode, pointId);
    }

    //获取AI最新报警
    private AlarmMessage getAIAlarmMessageLast(Long tenentId, String orgCode, String cameraName) {
        return alarmMessageMapper.getLastAIAlarmMeaasge(tenentId, orgCode, cameraName);
    }


    //大屏推送风险分区报警信息
    private void setWebSocketDistrictAlarmData(List<RiskEvaluationAlarmMesage> alarmList, String flagType) {
        List<MonitorWebSocketDistrictAlarmData> datas = new ArrayList<>();
        if (!CollectionUtils.isEmpty(alarmList)) {
            for (RiskEvaluationAlarmMesage meaasge : alarmList) {
                MonitorWebSocketDistrictAlarmData data = new MonitorWebSocketDistrictAlarmData();
                data.setDistrictId(meaasge.getRiskDistrictId());
                String name = riskDistrictDao.getNameById(meaasge.getRiskDistrictId());
                data.setDistrictName(name);
                data.setAlarmMessageId(meaasge.getId());
                data.setAlarmMessage("\"" + name + "\"" + meaasge.getDetail());
                data.setAlarmMessageTime(meaasge.getCreateTime());
                data.setFlagType(flagType);
                data.setOrgCode(meaasge.getOrgCode());
                data.setTenentId(meaasge.getTenentId());
                datas.add(data);
            }
        }
        MonitorPointDataWebSocket.sendInfo(datas, WebsocketDataType.DISTRICT_ALARM.getIndex());
    }

    //向大屏推送巡检排查报警信息
    private void setWebSocketInspectAlarmData(List<InspectExceptionAlarmMesage> inspectList, String flagType, Long tenentId, String orgCode) {
        List<MonitorWebSocketInspectAlarmData> datas = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inspectList)) {
            for (InspectExceptionAlarmMesage meaasge : inspectList) {
                MonitorWebSocketInspectAlarmData data = new MonitorWebSocketInspectAlarmData();
                data.setRiskPointId(meaasge.getRiskPointId());
                String name = riskPointMapper.getNameById(meaasge.getRiskPointId());
                data.setRiskPointName(name);
                data.setAlarmMessageId(meaasge.getId());
                data.setAlarmMessage(meaasge.getDetail());
                data.setAlarmMessageTime(meaasge.getAlarmTime());
                data.setFlagType(flagType);
                data.setOrgCode(meaasge.getOrgCode());
                data.setTenentId(meaasge.getTenentId());
                if (null != meaasge.getRiskPointId()) {
                    RiskPoint riskPoint = riskPointService.findById(meaasge.getRiskPointId());
                    //计算中心点
                    if (null != riskPoint && null != riskPoint.getCenterPoint()) {
                        MapUtils.SafetyPoint centerPoint = JsonUtil.fromJson(riskPoint.getCenterPoint(), MapUtils.SafetyPoint.class);
                        data.setX(centerPoint.getX());
                        data.setY(centerPoint.getY());
                        data.setZ(centerPoint.getZ());
                        data.setFloorId(riskPoint.getFloorId());
                    }
                }
                datas.add(data);
            }
        } else {
            MonitorWebSocketInspectAlarmData data = new MonitorWebSocketInspectAlarmData();
            data.setTenentId(tenentId);
            data.setOrgCode(orgCode);
            data.setFlagType(flagType);
            datas.add(data);
        }
        MonitorPointDataWebSocket.sendInfo(datas, WebsocketDataType.INSPECT_ALARM.getIndex());
    }

    //向大屏推送AI报警信息
    private void setWebSocketAIAlarmData(List<AlarmMessage> inspectList, String flagType, Long tenentId, String orgCode) {
        List<AlarmMessageVO> datas = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inspectList)) {
            for (AlarmMessage alarmMessage : inspectList) {
                AlarmMessageVO vo = new AlarmMessageVO();
                vo.setId(alarmMessage.getId());
                vo.setType("AI报警");
                vo.setAlarmTypeName("AI报警");
                vo.setSubTypeName(alarmMessage.getSubTypeName());
                vo.setAlarmInfo(alarmMessage.getAlarmInfo());
                BaseAttachmentBean baseAttachmentBean = new BaseAttachmentBean();
                if (null != alarmMessage.getAlarmPicId()) {
                    baseAttachmentBean.setId(alarmMessage.getAlarmPicId());
                    baseAttachmentBean.setDownloadUrl(serverUrl + "/image/view?id=" + alarmMessage.getAlarmPicId() + "&flag=aiAlarm");
                    vo.setAlarmPic(baseAttachmentBean);
                }
                vo.setCameraName(alarmMessage.getCameraName());
                vo.setStatus(alarmMessage.getStatus() == 0 ? "未处理" : "已处理");
                vo.setAlarmTime(alarmMessage.getAlarmTime());
                vo.setTenentId(alarmMessage.getTenentId());
                vo.setOrgCode(alarmMessage.getOrgCode());
                vo.setFlagType(flagType);
                vo.setModule(ApiAlarmTypeEnum.getNameById(alarmMessage.getAlarmType()));
                datas.add(vo);
            }
        } else {
            AlarmMessageVO vo = new AlarmMessageVO();
            vo.setTenentId(tenentId);
            vo.setOrgCode(orgCode);
            vo.setFlagType(flagType);
            datas.add(vo);
        }
        MonitorPointDataWebSocket.sendInfo(datas, WebsocketDataType.AI_ALARM.getIndex());
    }
}
